package game.metalmax.ui.main
{
	import fl.controls.Button;
	import fl.controls.UIScrollBar;
	
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	
	import game.metalmax.controller.Notifications;
	import game.metalmax.entity.main.Dialog;
	import game.metalmax.entity.main.DialogPhase;
	import game.metalmax.module.system.SystemManager;
	import game.metalmax.ui.common.Button001;
	import game.metalmax.ui.common.ScrollBar001;
	
	import showtime.framework.manager.AssetManager;
	import showtime.framework.manager.StyleManager;
	import showtime.framework.mvc.patterns.facade.Facade;
	import showtime.framework.ui.PrinterTextField;
	import showtime.framework.ui.Window;

	public class DialogWindow extends Window
	{
		private var acceptBtn:Button = null;
		private var refuseBtn:Button = null;
		
		private var submitBtn:Button = null;
		private var closeBtn:Button = null;
		
		private var contentTxt:PrinterTextField = null;		
		private var scrollBar:UIScrollBar = null; 
		
		public function DialogWindow()
		{
			super();
		}
		
		override protected function initialize():void
		{
			super.initialize();
			
			addEventListener(MouseEvent.CLICK, clickHandler);
			
			size(510, 170);
		}
		
		override protected function createChildren():void
		{
			var window:Sprite = new (AssetManager.getInstance().getClass("MAIN_SPRITE_MAIN_WINDOW"));
			
			window.width = 510;
			window.height = 170;
			addChild(window);
			window.alpha = 0.7;
			contentTxt = new PrinterTextField(20, updateScrollerHandler);
			contentTxt.move(30, 30);
			contentTxt.textField.defaultTextFormat = (StyleManager.getInstance().getStyleAsTextFormat("FONT_DIALOG"));
			contentTxt.textField.width = 445;			
			addChild(contentTxt);		
	
			scrollBar = new ScrollBar001();
			scrollBar.height = 100;			
			scrollBar.scrollTarget = contentTxt.textField;
			scrollBar.move(480, 20);
			scrollBar.validateNow();
			addChild(scrollBar);
			
			
			closeBtn = new Button001();
			closeBtn.label = "停止对话";
			closeBtn.move(20, 135);
			closeBtn.width = 84;
			closeBtn.height = 25;
			
			closeBtn.textField.textColor = 0xFFFFFF;
			
			addChild(closeBtn);
			
			submitBtn = new Button001();
			submitBtn.label = "确定";
			submitBtn.move(430, 135);
			submitBtn.width = 50;
			submitBtn.height = 25;
			addChild(submitBtn);
			
			acceptBtn = new Button001();
			acceptBtn.label = "接受";
			acceptBtn.move(370, 135);
			acceptBtn.width = 50;
			acceptBtn.height = 25;
			addChild(acceptBtn);
			
			refuseBtn = new Button001();
			refuseBtn.label = "拒绝";
			refuseBtn.move(430, 135);
			refuseBtn.width = 50;
			refuseBtn.height = 25;			
			addChild(refuseBtn);
			
			function updateScrollerHandler():void
			{
				scrollBar.update();
			}
		
		}
		
		override public function open():void
		{			
			
		}
		
		override public function refresh(data:Object=null):void
		{		
			setData(data);
			var phase:DialogPhase = data.phases[data.nowIndex];
			
			
			contentTxt.text = phase.content;
				
			switch (phase.type)
			{
				case Dialog.NORMAL:
				{
					submitBtn.visible = true;
					acceptBtn.visible = false;
					refuseBtn.visible = false;
					break;
				}
				case Dialog.CHOICE:
				{
					submitBtn.visible = false;
					acceptBtn.visible = true;
					refuseBtn.visible = true;
					break;
				}
			}
			
			contentTxt.start();
		}
		
		override public function close():void
		{
			setData(null);
		}
		
		private function clickHandler(evt:MouseEvent):void
		{
			contentTxt.completeNow();

			if (evt.target == closeBtn)
			{
				contentTxt.stop();			
				Facade.getInstance().sendNotification(Notifications.CLOSE_DIALOG_WINDOW);	
			}
			else if (evt.target == submitBtn)
			{			
				SystemManager.getInstance().executeScript(getData().phases[getData().nowIndex].scriptID, 0);
				contentTxt.stop();				
				Facade.getInstance().sendNotification(Notifications.CLOSE_DIALOG_WINDOW);
	
				
			}
			else if (evt.target == acceptBtn)
			{
				getData().nowIndex = getData().phases[getData().nowIndex].accept;
				refresh(getData());
			}
			else if (evt.target == refuseBtn)
			{
				getData().nowIndex = getData().phases[getData().nowIndex].refuse;
				refresh(getData());
			}
		}
	}
}